package com.shinow.capacitor.plusins.printer.zebra;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;

import androidx.appcompat.app.AppCompatActivity;

public class FontCompoundImage {
    private static float ROTATE = 0F;

    public static Bitmap createImage(String str, int size) {
        return createImage(str, size, ROTATE);
    }

    public static Bitmap createImage(String str, int size, float rotate) {
        Paint paint = new Paint();
        paint.setTextSize(size);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.parseColor("#000000"));

        return createImage(paint, str, rotate);
    }

    public static Bitmap createImage(Paint paint, String str, float rotate) {
        paint.setDither(true);
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);

        int fontWidth = Math.round(paint.measureText(str));
        int ascent = Double.valueOf(Math.ceil(Math.abs(paint.getFontMetrics().ascent))).intValue();
        int descent = Double.valueOf(Math.ceil(Math.abs(paint.getFontMetrics().descent))).intValue();
        int fontHeight = ascent + descent;

        Bitmap bitmap = Bitmap.createBitmap(fontWidth, fontHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawText(str, 0, ascent, paint);
        canvas.save();
        canvas.restore();

        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
}
